<%-- 
    Document   : index
    Created on : 2019-9-24, 10:51:56
    Author     : 10592
--%>

<%@page import="web.enity.UserInfo"%>
<%@page import="web.Dao.impl.UserDAOImpl"%>
<%@page import="web.Dao.UserDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%!
    String username;
    String pwd;
    
%>
<%
    //通过cookie自动登录
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("username")) {
                username = cookie.getValue();
            } else if (cookie.getName().equals("pwd")) {
                pwd = cookie.getValue();
            }
        }
        if (username != null && pwd != null) {
            //数据库验证
            UserDAO userDao = new UserDAOImpl();
            UserInfo user = userDao.login(username, pwd);
           
            
        }
        else {
                response.sendRedirect("Login.html");
            }
    }

%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link type="text/css" rel="stylesheet" href="css/style.css" />
    </head>
    <body>
        <div id="header" class="wrap">
            <div id="logo">网上书城</div>
            <div id="navbar">
                <div class="userMenu">
                    <ul>
                        <li class="current"><a href="index.jsp">User首页</a></li>
                        <li><a href="order.html">我的订单</a></li>
                        <li><a href="shopping.jsp?action=add">购物车</a></li>
                        <li><a href="#">注销</a></li>
                    </ul>
                </div>
                <form method="get" name="search" action="">
                    搜索：<input class="input-text" type="text" name="keywords" /><input class="input-btn" type="submit" name="submit" value="" />
                </form>
            </div>
        </div>
        <div id="content" class="wrap">
            <div class="list bookList">
                    <c:if test="${empty resultList}">
                        <h1>目前没有任何商品</h1>
                    </c:if>
                    <c:if test="${not empty resultList}"> 
                        <h1>目前有商品</h1>
                        <form action="shopping.html?action=add" method="post" id="books">
                            <table>
                                <tr class="title">
                                    <th class="checker"></th>
                                    <th>书名</th>
                                    <th class="price">价格</th>
                                    <th class="store">库存</th>
                                    <th class="view">图片预览</th>
                                </tr>
                                <c:forEach items="${resultList}" var="b" varStatus="status">
                                    <tr class="odd">
                                        <td><input type="checkbox" name="bookId" value="${b.bid}" id="Bookid"/></td>
                                        <td class="title">${b.bookName}</td>
                                        <td>￥${b.price}0</td>
                                        <td>${b.stock}</td>
                                        <td class="thumb"><img src="${b.image}" /></td>
                                    </tr>
                                </c:forEach>

                            </table>
                        </form>

                        <!--分页控件-->
                        <div class="page-spliter">
                            <form action="index.html" method="post" id="pageForm">
                               
                                <input type="hidden" value="${pager.currentPage}" id="page" name="page"/>
                                <a href="javascript:pre()">&lt;</a>
                                <a href="javascript:head()">首页</a>
                                <c:forEach begin="1" end="${pager.pageCount}" var="i" >
                                    <c:if test="${pager.currentPage==i}"> 
                                        <span class="current">${pager.currentPage}</span>
                                    </c:if>
                                    <c:if test="${pager.currentPage!=i}"> 
                                        <a href="index.html?page=${i}">${i}</a>
                                    </c:if>
                                </c:forEach>
                                <a href="javascript:end()">尾页</a>
                                <a href="javascript:next()">&gt;</a>

                            </form>
                           
                        </div>
                            
                               
                    </c:if>
                        
                        <div class="button"><input class="input-btn" type="submit" name="submit" onclick="addToCart()" value=""/></div>
            </div>
        </div>
        <div id="footer" class="wrap">
            网上书城 &copy; 版权所有

        </div>
    </body>
    <script>
        function head(){
            var p=1;
            document.getElementById("page").value=p;
            console.log(p);
            document.getElementById("pageForm").submit();
        }
        function pre() {
            var p = document.getElementById("page").value;
            if (p>1) {
                var pg = parseInt(p);
                document.getElementById("page").value = p - 1;
            }
            console.log(p);
            document.getElementById("pageForm").submit();
        }
        function next() {
                var p = document.getElementById("page").value;
                if (p <${pager.pageCount}) {
                    var pg = parseInt(p);
                    document.getElementById("page").value = pg + 1;
                }
                console.log(p);
                document.getElementById("pageForm").submit();
            }
        function end(){
             var p=${pager.pageCount};
             document.getElementById("page").value=p;
             console.log(p);
             document.getElementById("pageForm").submit();
         }
        function addToCart(){  
             document.getElementById("books").submit();  
    }  
    </script>
</html>
